package websimulator;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import buildings.House;

import readwrite.Connector;

/**
 * Servlet implementation class ModulList
 */
@WebServlet("/ModulList")
public class ModulList extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private Connector con; 
    /**
     * @throws Exception 
     * @see HttpServlet#HttpServlet()
     */
    public ModulList() throws Exception {
        super();
        con = new Connector();
        // TODO Auto-generated constructor stub
    }

	/**
	 * Lists all houses assosieted with the modul.
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try{
		String answer="";
		int modulId = con.GetLastModulID();
		ArrayList<House> house = new ArrayList<House>();
		if(con.getHouseModuls(modulId) != null)
		{
			house = con.getHouseModuls(modulId);
			answer = "<div id=\"modulList\">";
			answer += "<ul>";
			answer += "<h3>List of Houses in Modul</h3>";
			answer += "<h4>Area,\t\tPopulation/Employees</h4>";
			for(int i = 0; i < house.size(); i++)
				answer+= "<li>"+ house.get(i).getArea() +" m^2"+"\t"+ house.get(i).getPeople() +"</li>";
			answer +="</ul>";
			answer +="<input type=\"button\" value=\"Update List\" onclick=\"updateList()\">";
			answer += "</div>";
		}
		else
		{
			answer = "<div id=\"modulList\">";
			answer += "<ul>";
			answer += "<h4>Area\tPopulation/Employees</h4>"; 
		}
		response.setContentType("text/plain");
		response.getWriter().write(answer);
		}
		catch(SQLException sql)
		{} 
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
